/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.coders;

import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import org.latestbit.smartobjects.exceptions.DataTypesCoderException;
import org.latestbit.smartobjects.exceptions.SmartBusSystemException;
import org.latestbit.smartobjects.io.ResizableByteBuffer;
import org.latestbit.smartobjects.network.INetworkCommand;

public class CallInterfaceOperationResponse extends StdNetworkCommand {

	public static final byte COMMAND_ID = CallInterfaceOperationRequest.COMMAND_ID + 1;
	
	enum ResponseFlags {		
		IS_EXCEPTION (1),
		INSTANCE_OBJECT_REF (2);

		private int value;
		
		ResponseFlags(int value) {
			this.value = value;
		}
		
		public int getValue() {
			return this.value;
		}
	}
	
	private byte responseFlags;
	
	
	private String exceptionInterfaceId;
	private String exceptionMessage;
	private String instanceObjectRef;
	
	public CallInterfaceOperationResponse(ResizableByteBuffer writeBuffer, IDataTypesCoder coder) {
		super(COMMAND_ID, writeBuffer, coder);
	}
	
	public CallInterfaceOperationResponse(INetworkCommand command) {
		super( command );
	}

	public void setExceptionFlag(boolean flag) {
		if(flag)
			this.responseFlags = (byte) (this.responseFlags | ResponseFlags.IS_EXCEPTION.getValue());
		else
			this.responseFlags = (byte) (this.responseFlags & ~ ResponseFlags.IS_EXCEPTION.getValue());		
	}

	public boolean isExceptionFlag() {
		return (this.responseFlags & ResponseFlags.IS_EXCEPTION.getValue()) !=0;
	}
	
	public void setInstanceObjectRefFlag(boolean flag) {
		if(flag)
			this.responseFlags = (byte) (this.responseFlags | ResponseFlags.INSTANCE_OBJECT_REF.getValue());
		else
			this.responseFlags = (byte) (this.responseFlags & ~ ResponseFlags.INSTANCE_OBJECT_REF.getValue());		
	}

	public boolean isInstanceObjectRefFlag() {
		return (this.responseFlags & ResponseFlags.INSTANCE_OBJECT_REF.getValue()) !=0;
	}

	public void setExceptionInterfaceId(String exceptionInterfaceId) {
		this.exceptionInterfaceId = exceptionInterfaceId;
	}

	public String getExceptionInterfaceId() {
		return exceptionInterfaceId;
	}
	
	public void setExceptionMessage(String exceptionMessage) {
		this.exceptionMessage = exceptionMessage;
	}

	public String getExceptionMessage() {
		return exceptionMessage;
	}	

	public void setInstanceObjectRef(String instanceObjectRef) {
		this.instanceObjectRef = instanceObjectRef;
	}

	public String getInstanceObjectRef() {
		return instanceObjectRef;
	}

	@Override
	public void beginSerialize() throws SmartBusSystemException {
		super.beginSerialize();
		this.coder.encodeUByte(responseFlags, this.buffer);
		if(isExceptionFlag()) {
			this.coder.encodeString(exceptionInterfaceId, this.buffer);
			this.coder.encodeString(exceptionMessage, this.buffer);
		}
		else {
			if( isInstanceObjectRefFlag() ) {
				this.coder.encodeString(this.instanceObjectRef, this.buffer);
			}			
		}
	}
	
	@Override
	public void deserialize() throws DataTypesCoderException, BufferUnderflowException {
		if(this.getCommandId()!=COMMAND_ID) {
			throw new DataTypesCoderException("Invalid command type id detected ("+this.getCommandId()+" instead "+COMMAND_ID+")!");
		}
		ByteBuffer readBuffer = this.buffer.getBuffer();
		this.responseFlags = coder.decodeUByte(readBuffer);
		if(isExceptionFlag()) {
			this.exceptionInterfaceId = coder.decodeString(readBuffer);
			this.exceptionMessage = coder.decodeString(readBuffer);
		}
		else {
			if( isInstanceObjectRefFlag() ) {
				this.instanceObjectRef = coder.decodeString(readBuffer);
			}			
		}		
	}
}
